package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.HandleOrderDto;
import com.huaxiafinance.management.mapper.PackageOrderMapper;
import com.huaxiafinance.management.model.PackageOrder;
import com.huaxiafinance.management.service.PartnerPassService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;


@Controller("partnerPassController")
@RequestMapping("/partnerPass")
@SuppressWarnings("SpringJavaAutowiringInspection")
public class PartnerPassController {

  private static final Logger log = Logger.getLogger(PartnerPassController.class);
  @Autowired
  private PartnerPassService partnerPassService;
  @Autowired
  private PackageOrderMapper packageOrderMapper;


  @RequestMapping(value = "/pass", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryMerchantList(String userUuid) {
    return partnerPassService.updateIsPartner(userUuid).getAsJSON();
  }


  @RequestMapping(value = "/handle", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String handle(@Valid HandleOrderDto dto, BindingResult bindingResult) {
    MessageResult result = new MessageResult();
    if (bindingResult.hasErrors()) {
      result.setFailureResult(bindingResult);
    }
    try {
      result = partnerPassService.handle(dto);
    } catch (Exception e) {
      log.error("处理失败", e);
      result.setFailureResult();
    }
    return result.getAsJSON();
  }

  @RequestMapping(value = "/updateHandle", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateHandle(@Valid HandleOrderDto dto, BindingResult bindingResult) {
    MessageResult result = new MessageResult();
    if (bindingResult.hasErrors()) {
      result.setFailureResult(bindingResult);
      return result.getAsJSON();
    }
    try {
      result = partnerPassService.updateHandle(dto);
    } catch (Exception e) {
      log.error("修改失败", e);
      result.setFailureResult();
    }
    return result.getAsJSON();
  }

  @RequestMapping(value = "/getOrderInfo", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getOrderInfo(Integer id) {
    MessageResult result = new MessageResult();
    PackageOrder packageOrder = packageOrderMapper.selectByPrimaryKey(id);
    log.info("套餐查询返回数据：" + packageOrder.toString());
    result.setData(packageOrder);
    result.setSuccessResult();
    return result.getAsJSON();

  }


}
